package me.luger.core.mvc.controller;

import com.google.common.base.Strings;
import me.luger.core.mvc.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.springframework.web.servlet.view.xml.MappingJackson2XmlView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * BaseController
 *
 * @author luger
 * @date 2016/8/8 0008
 */
public abstract class BaseController {

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }

//    @InitBinder
//    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        dateFormat.setLenient(false);
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
//    }

    public String getPara(String name){
        return getPara(name,"");
    }

    public String getPara(String name,String defaultVal){
        String value = request.getParameter(name);
        if(Strings.isNullOrEmpty(value)){
            return defaultVal;
        }
        return value;
    }

    public BaseController setAttr(String name, Object value){
        this.request.setAttribute(name,value);
        return this;
    }

    public BaseController removeAttr(String name) {
        this.request.removeAttribute(name);
        return this;
    }

    public BaseController setAttrs(Map<String, Object> attrMap) {
        Iterator iterator = attrMap.entrySet().iterator();

        while(iterator.hasNext()) {
            Map.Entry entry = (Map.Entry)iterator.next();
            this.request.setAttribute((String)entry.getKey(), entry.getValue());
        }
        return this;
    }

    public ModelAndView btlView(String viewPath){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(viewPath);
        return modelAndView;
    }

    public ModelAndView jspView(String viewPath){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp:"+viewPath);
        return modelAndView;
    }

    public ModelAndView thView(String viewPath){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("th:"+viewPath);
        return modelAndView;
    }

    public ModelAndView jsonView(Object objData){
        if(getPara("rformat").equals("xml")){
            return xmlView(objData);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(new MappingJackson2JsonView());
        modelAndView.getModelMap().addAttribute(objData);
        return modelAndView;
    }

    public ModelAndView xmlView(Object objData){
        if(getPara("rformat").equals("json")){
            return jsonView(objData);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(new MappingJackson2XmlView());
        modelAndView.getModelMap().addAttribute(objData);
        return modelAndView;
    }
}
